﻿using SqlSugar;
using System.Linq.Expressions;

namespace Infrastructure
{
    public class MesRepository<T>:SimpleClient<T> where T:class,new()
    {
        private readonly ISqlSugarClient db;

        
        public MesRepository(ISqlSugarClient db)
        {
            this.db = db;
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Add(T entity)
        {
            return db.Insertable(entity).ExecuteCommand();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(T entity)
        {
            return db.Updateable(entity).ExecuteCommand();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Delete(T entity)
        {
            return db.Deleteable(entity).ExecuteCommand();
        }
        /// <summary>
        /// 获取单个
        /// </summary>
        public T GetEntity(Expression<Func<T,bool>> expression)
        {
            return db.Queryable<T>().First(expression);
        }
        /// <summary>
        /// 获取列表（慎用）
        /// </summary>
        public List<T> GetList()
        {
            return db.Queryable<T>().ToList();
        }
        /// <summary>
        /// 根据条件获取查询对象
        /// </summary>
        public ISugarQueryable<T> GetQueryable(Expression<Func<T, bool>> expression)
        {
            return db.Queryable<T>().Where(expression);
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int BatchUpdate(List<T> list)
        {
            return db.Updateable(list).ExecuteCommand();
        }
    }
}
